using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YieldReturn
{
    public float time;
    public IEnumerator ie;
    public YieldReturn(float time = 0, IEnumerator ie = null)
    {
        this.time = time;
        this.ie = ie;
    }
}
public class CourtinrMge : MonoBehaviour
{
    // Start is called before the first frame update
    
    List<YieldReturn> list = new List<YieldReturn>();
    private static CourtinrMge instance;
    public static CourtinrMge Instance =>instance;
    void Awake()
    {
        instance = this;
    }
    void Start()
    {
        
    }
    public void StartMyCourtine(IEnumerator ie)
    {
        if (ie.MoveNext())
        {
            if(ie.Current is int)
            {

                YieldReturn yieldReturn=new YieldReturn(Time.time + (int)ie.Current,ie);
                list.Add(yieldReturn);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        for (int i = list.Count - 1; i >= 0; i--)
        {
            if (list[i].time<=Time.time)
            {
                if (list[i].ie.MoveNext())
                {
                    if(list[i].ie.Current is int)
                    {
                        list[i].time = Time.time+(int)list[i].ie.Current;
                    }
                    else
                    {
                        list.RemoveAt(i);
                    }
                }
                else
                {
                    list.RemoveAt(i);
                }
            }
        }
    }
}
